package com.tone.ji_volunteer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tone.ji_volunteer.mapper.ActivityMapper;
import com.tone.ji_volunteer.mapper.UserMapper;
import com.tone.ji_volunteer.pojo.Activity;
import com.tone.ji_volunteer.pojo.Joiner;
import com.tone.ji_volunteer.pojo.PageBean;
import com.tone.ji_volunteer.pojo.User;
import com.tone.ji_volunteer.service.ActivityLogService;
import com.tone.ji_volunteer.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tone.ji_volunteer.pojo.Log;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @author tone(ang)
 * version 1.0
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ActivityLogService activityLogService;

    @Override
    public PageBean list(Integer page, Integer pageSize, String query) {
        //使用pageHelper

        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行分页查询
        List<Activity> list = activityMapper.list();
        //获取分页结果
        Page<Activity> p = (Page<Activity>) list;
        // 封装结果
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public Activity selectActivity(String openid, Integer activityId) {
        Activity activity = activityMapper.select(activityId);
        //System.out.println(activity.getOpenid());
        //System.out.println(openid);
        if(openid != null && !openid.equals(activity.getOpenid())) activity.setOpenid("no");
        else activity.setOpenid("yes");
        return activity;
    }

    @Override
    public List<Activity> activitySearch(String query) {
        List<Activity> activities = activityMapper.search(query);
        return activities;
    }

    @Override
    @com.tone.ji_volunteer.anno.Log
    public void addImages(String upload, Integer activityID) {
        //根据openid 找到用户id，在添加到Image中
        activityMapper.addImages(upload, activityID);
    }

    @Override
    @com.tone.ji_volunteer.anno.Log
    public Integer addActivity(Map<String, Object> activity, String openid) {
        Activity act = new Activity();
        act.setTitle((String) activity.get("title"));
        act.setContent((String) activity.get("content"));
        act.setProvince((String) activity.get("province"));
        act.setCity((String) activity.get("city"));
        act.setRegion((String) activity.get("region"));
        act.setDetail((String) activity.get("detail"));
        act.setContactInfo((String) activity.get("contactInfo"));
        act.setCreateTime(LocalDateTime.now());
        LocalDate endDate = LocalDate.parse((String)activity.get("endDate"), DateTimeFormatter.ISO_LOCAL_DATE);
        LocalTime time = LocalTime.parse((String)activity.get("endTime"), DateTimeFormatter.ISO_LOCAL_TIME);
        // 合并为 LocalDateTime
        LocalDateTime endTime = LocalDateTime.of(endDate, time);
        act.setEndTime(endTime);
        //找到创建者
        User user = userMapper.select(openid);
        System.out.println(user);
        act.setUserId(user.getId());
        act.setState((short)1);
        activityMapper.addActivity(act);
        return act.getId();
    }

    @Override
    public List<String> getImages(Integer activityId) {
        return  activityMapper.getImages(activityId);
    }

    @Override
    public List<Activity> myFoundActivity(String openid) {
        Integer userId = userMapper.select(openid).getId();
        List<Activity> founder = activityMapper.founder(userId);
        return founder;
    }

    @Override
    @com.tone.ji_volunteer.anno.Log
    public void joinActivity(String openid, Integer activityId) {
        Integer userId = userMapper.select(openid).getId();
        activityMapper.join(userId, activityId);
    }

    @Override
    public Joiner isJoin(String openid, Integer activityId) {
        Integer userId = userMapper.select(openid).getId();
        return activityMapper.isJoin(userId, activityId);
    }

    @Override
    @com.tone.ji_volunteer.anno.Log
    public List<Activity> myParticipation(String openid) {
        Integer userId = userMapper.select(openid).getId();
        return activityMapper.participation(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @com.tone.ji_volunteer.anno.Log
    public void deleteActivity(Integer id) {
        System.out.println("id=" + id);
        try{
            //删除活动
            activityMapper.delete(id);
            //删除照片
            activityMapper.deleteImage(id);
            //删除参与者
            activityMapper.deleteJoiner(id);
        }finally {
            Log log = new Log();
            log.setCreateTime(LocalDateTime.now());
            log.setDescription("创建者删除了id=" + id + "的活动");
            activityLogService.insert(log);
        }

    }

    @Override
    @com.tone.ji_volunteer.anno.Log
    public void endActivity(Integer activityId) {
        System.out.println(activityId);
        activityMapper.endActivity(activityId, LocalDateTime.now());
    }
}
